define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'nursery/wantbuyoffer/index' + location.search,
                    add_url: 'nursery/wantbuyoffer/add',
                    edit_url: 'nursery/wantbuyoffer/edit',
                    del_url: 'nursery/wantbuyoffer/del',
                    multi_url: 'nursery/wantbuyoffer/multi',
                    import_url: 'nursery/wantbuyoffer/import',
                    table: 'nursery_wantbuy_offer',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                // search:false,
                // commonSearch:true,
                // searchFormVisible:false,
                // searchFormTemplate:'customformtpl',
                queryParams:function (params){
                    var filter=JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    var id=Fast.api.query('ids');
                    console.log(id)
                    if (id){
                        filter.wantbuy_id=id;
                        op.wantbuy_id='=';
                        params.filter=JSON.stringify(filter);
                        params.op = JSON.stringify(op);
                        funcUrlDel('ids');
                    }
                    return params;
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'user_id', title: __('User_id')},
                        // {field: 'wantbuy_id', title: __('Wantbuy_id')},
                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'num', title: __('Num')},
                        {field: 'price', title: __('Price'), operate: 'LIKE'},
                        {field: 'company_name', title: __('Company_name'), operate: 'LIKE'},
                        {field: 'phone', title: __('Phone'), operate: 'LIKE'},
                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                        {field: 'disable', title: __('Disable'), searchList: {"0":__('Disable 0'),"1":__('Disable 1')}, formatter: Table.api.formatter.normal},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            function funcUrlDel(name){
                var loca = window.location;
                var baseUrl = loca.origin + loca.pathname + "?";
                var query = loca.search.substr(1);

                if (query.indexOf(name)>-1) {
                    var obj = {};
                    var arr = query.split("&");
                    for (var i = 0; i < arr.length; i++) {
                        arr[i] = arr[i].split("=");
                        obj[arr[i][0]] = arr[i][1];
                    };
                    delete obj[name];
                    var url = baseUrl + JSON.stringify(obj).replace(/[\"\{\}]/g,"").replace(/\:/g,"=").replace(/\,/g,"&");
                    history.pushState(null,null,url);
                    return;
                }else{
                    return window.location.href;
                };
            }


            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
